import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_core/app_core.dart';
import '../../../common/managers/theme_manager/theme_manager.dart';
import '../../../common/constant/colors.dart';
import '../../../common/constant/icon.dart';
import '../../../common/constant/styles.dart';

class MessagePage extends StatelessWidget {
  const MessagePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Get.theme.colorScheme.background,
      appBar: MyAppBar(
        '消息',
        showBack: false,
        // color: Get.theme.colorScheme.surface,
        titleColor: Get.theme.colorScheme.onSurface,
        actions: [
          AppGesture(
            onTap: () {
              Get.log('搜索消息');
            },
            child: Padding(padding: EdgeInsets.all(8), child: Icon(Icons.search, color: Get.theme.colorScheme.onPrimary, size: 24)),
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: AppSpaces.edgeMd,
        child: Column(
          children: [
            _buildMessageItem(name: '系统通知', message: '您的设备已成功连接', time: '10:30', unreadCount: 2, isSystem: true),
            _buildMessageItem(name: '设备提醒', message: '客厅灯已开启超过2小时', time: '09:15', unreadCount: 1, isSystem: true),
            _buildMessageItem(name: '安全提醒', message: '检测到异常登录行为', time: '昨天', unreadCount: 0, isSystem: true),
            _buildMessageItem(name: '更新通知', message: '新版本已发布，建议立即更新', time: '2天前', unreadCount: 0, isSystem: true),
            _buildMessageItem(name: '活动通知', message: '限时优惠活动正在进行中', time: '3天前', unreadCount: 0, isSystem: true),
            _buildMessageItem(name: '设备离线', message: '卧室空调已离线，请检查连接', time: '3天前', unreadCount: 0, isSystem: false),
            _buildMessageItem(name: '定时任务', message: '定时任务"晚安模式"已执行', time: '4天前', unreadCount: 0, isSystem: false),
          ],
        ),
      ),
    );
  }

  Widget _buildMessageItem({required String name, required String message, required String time, required int unreadCount, required bool isSystem}) {
    return Container(
      margin: EdgeInsets.only(bottom: 12),
      decoration: BoxDecoration(color: AppColor.white, borderRadius: AppSpaces.borderRadiusMd),
      child: AppGesture(
        onTap: () {
          Get.snackbar('提示', '消息详情功能开发中...');
        },
        child: Padding(
          padding: AppSpaces.edgeMd,
          child: Row(
            children: [
              // 头像
              Container(
                width: 48,
                height: 48,
                decoration: BoxDecoration(color: isSystem ? AppColor.info : AppColor.textSecondary, borderRadius: AppSpaces.borderRadiusMd),
                child: Icon(isSystem ? Icons.notifications : Icons.devices, color: AppColor.white, size: 24),
              ),
              SizedBox(width: 16),

              // 消息内容
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(child: Text(name, style: AppStyles.s16w5.copyWith(fontWeight: unreadCount > 0 ? FontWeight.bold : FontWeight.w500))),
                        Text(time, style: AppStyles.s12.copyWith(color: AppColor.textSecondary)),
                      ],
                    ),
                    SizedBox(height: 4),
                    Text(
                      message,
                      style: AppStyles.s14.copyWith(color: unreadCount > 0 ? AppColor.primary : AppColor.textSecondary),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              ),

              // 未读消息数量
              if (unreadCount > 0) ...[
                SizedBox(width: 8),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                  decoration: BoxDecoration(color: AppColor.error, borderRadius: BorderRadius.circular(12)),
                  child: Text(unreadCount.toString(), style: AppStyles.s12.copyWith(color: AppColor.white, fontWeight: FontWeight.bold)),
                ),
              ],
            ],
          ),
        ),
      ),
    );
  }
}
